﻿using IDLETD.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using IDLETD.UserControls;
using System.Windows.Threading;
using IDLETD.Agent;

namespace IDLETD
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        //ObservableCollection<MonsterControl> monsterList = new ObservableCollection<MonsterControl>();
        //ObservableCollection<TowerControl> towerList = new ObservableCollection<TowerControl>();

        ObservableCollection<BaseGameObject> objectList = new ObservableCollection<BaseGameObject>();


        Storyboard storyboard = new Storyboard();

        List<Point> Path = new List<Point>() { new Point(400, 0), new Point(400, 50), new Point(100, 50), new Point(100,450), 
            new Point(250,450), new Point(250, 200), new Point(650, 200), new Point(650, 450), new Point(400, 450), new Point(400, 500) };
        DispatcherTimer Timer;

        public MainWindow()
        {
            InitializeComponent();
            objectList.CollectionChanged += ObjectList_CollectionChanged;
        }

        private void ObjectList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    foreach (BaseGameObject obj in e.OldItems)
                    {
                        for (int i = 0; i < Canvas_Main.Children.Count; i++)
                        {
                            if ((Canvas_Main.Children[i] as BaseControl).DataInfo.Id == obj.Id)
                            {
                                Canvas_Main.Children.RemoveAt(i--);
                            }
                        }
                    }
                    break;
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            foreach (var obj in objectList.ToList())
            {
                if (obj.IsDestroy)
                {
                    objectList.Remove(obj);
                    continue;
                }
                obj.Excute();
            }
        }

        private void CreateBorder()
        {
            MonsterModel monster = new MonsterModel() { Hp = 5 };
            MonsterControl monsterControl = new MonsterControl() { DataInfo = monster };
            monsterControl.crashAgnet.ObjectList = objectList;
            objectList.Add(monster);
            Canvas_Main.Children.Add(monsterControl);
            monster.GoMove(Path);
        }

        private void CreateTower(double x, double y)
        {
            TowerModel tower = new TowerModel();
            TowerControl towerControl = new TowerControl() { DataInfo = tower  };
            towerControl.TowerShotBullet += TowerControl_TowerShotBullet;
            towerControl.crashAgnet.ObjectList = objectList;
            tower.X = x - tower.Width / 2;
            tower.Y = y - tower.Height / 2;
            objectList.Add(tower);
            Canvas_Main.Children.Add(towerControl);
            tower.GoScan();
        }

        private void TowerControl_TowerShotBullet(BulletModel bullet, MonsterModel monster)
        {
            BulletControl bulletControl = new BulletControl() { DataInfo = bullet };
            bulletControl.crashAgnet.ObjectList = objectList;
            Canvas.SetLeft(bulletControl, bullet.X);
            Canvas.SetTop(bulletControl, bullet.Y);
            objectList.Add(bullet);
            Canvas_Main.Children.Add(bulletControl);
            bullet.GoFollow(monster);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Timer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 1000 / 60) };
            Timer.Tick += Timer_Tick;
            Timer.Start();

            DispatcherTimer creater = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 1000) };
            creater.Tick += Creater_Tick;
            creater.Start();

            CreateTower(200, 200);
            //CreateBorder();
        }

        private void Creater_Tick(object sender, EventArgs e)
        {
            CreateBorder();
        }
    }
}
